package net.borlis.sample.web.weld;

import net.borlis.sample.web.context.WeldContainer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author alex@borlis.net
 */
public class WeldInitServlet extends HttpServlet {

    private static final Logger LOGGER = Logger.getLogger(WeldInitServlet.class.getName());
    // -=-=-=-
    private static final String H6 = "<h6>";
    private static final String SEPARATOR = H6 + " -- " + H6;

    public void init() throws ServletException {
        // Do required initialization
        LOGGER.log(Level.INFO, "The Weld CDI is trying to init...");
        LOGGER.log(Level.INFO, "the context initiated ", WeldContainer.getInstance().getContext());
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html");
        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        // -=-=-=-
        out.println(H6 + "the weld init servlet" + H6);
        // -=-=-=-
        out.println(SEPARATOR);
        out.println(H6 + " state = " + WeldContainer.getInstance().getContext().isInit() + H6);
        // -=-=-=-
        out.println(SEPARATOR);
        out.println(H6 + " schema = " + WeldContainer.getInstance().getContext().getConfiguration().getSchemaVersion() + H6);
        out.println(H6 + " database server = " + WeldContainer.getInstance().getContext().getConfiguration().getServer() +
                ":" + WeldContainer.getInstance().getContext().getConfiguration().getPort() + H6);
        out.println(H6 + " database driver = " + WeldContainer.getInstance().getContext().getConfiguration().getDriverName()+ H6);
        out.println(H6 + " database user = " + WeldContainer.getInstance().getContext().getConfiguration().getJdbcUser() + H6);
        out.println(H6 + " database auto update = " + WeldContainer.getInstance().getContext().getConfiguration().getAutoUpdate() + H6);
        // -=-=-=-
        out.println(SEPARATOR);
        out.println(H6 + " encryption service URL = " + WeldContainer.getInstance().getContext().getConfiguration().getEncryptionServiceURL() + H6);
        out.println(H6 + " encryption service login = " + WeldContainer.getInstance().getContext().getConfiguration().getEncryptionServiceLogin() + H6);
        out.println(SEPARATOR);
    }

}